<?php

namespace server\dao;

use common\helpers\ConstantHelper;
use Yii;
use yii\mongodb\Query;

/**
 * This is the model class for collection "lane_devices".
 *
 * @property \MongoDB\BSON\ObjectID|string $_id
 */
class LaneDevices extends \common\core\server\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function collectionName()
    {
        //return 'devices';
        return 'lane_devices';
    }

    /**
     * {@inheritdoc}
     */
    public function attributes()
    {
        return [
            '_id',
            'line_id',
            'station_id',
            'lane_id',
            'name',
            'ip',
            'control_ip',
            'remote_username',
            'remote_password',
            'status',
            'created_at',
            'updated_at'
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [

        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            '_id' => 'ID',
            'line_id' => 'Line ID',
            'station_id' => 'station ID',
            'lane_id' => 'lane ID',
            'ip' => 'IP',
            'control_ip' => 'Control IP',
            'name' => 'Name',
            'remote_username' => 'Remote Username',
            'remote_password' => 'Remote Password',
            'status' => 'Status',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
        ];
    }


    public function getDevicesList($params = [], $isAll = true)
    {
        $query = new Query();
        $query->from($this->collectionName());
        if ($this->issetAndEmpty($params, 'type')) {
            if ($params['type'] == 1) {
                $query->andWhere(['line_id' => $params['id']]);
            } elseif ($params['type'] == 2) {
                $query->andWhere(['station_id' => $params['id']]);
            }
        }
        if ($this->issetAndEmpty($params, 'ip')) {
            $query->andWhere(['uuid' => $params['ip']]);
        }
        //return $query->createCommand()->getRawSql();
        return $isAll ? $query->all() : $this->mongodbPage($query);
    }

    public function getInfoByIP($ip){
        $query = new Query();
        return $data = $query
            ->select(['line_id','station_id','lane_id','control_ip','ip','name'])
            ->where(['ip'=>$ip])
            ->from($this->collectionName())
            ->one();
    }

    public function insertDevices($params)
    {
        $collection = \Yii::$app->mongodb->getCollection($this->collectionName());
        //返回添加后的objectId，不限制于attributes
        return $collection->insert($params);

    }

    public function updateDevices($params)
    {
        //var_dump($params);exit();
        $id = $params['_id'];
        unset($params['_id']);
        $collection = \Yii::$app->mongodb->getCollection($this->collectionName());
        //返回添加后的objectId，不限制于attributes
        return $collection->update(['_id'=>$id],$params);

    }

    public function deleteDevices($params){
        $collection = \Yii::$app->mongodb->getCollection($this->collectionName());
        //返回添加后的objectId，不限制于attributes
        return $res = $collection->remove(['_id'=>$params['id']]);
    }

    public function getAllInfo($isAll = true){
        $query = new Query();
        $query->from($this->collectionName());
        $query->select(['line_id','station_id','lane_id','ip','name','control_ip','protocol','remote_username','remote_password']);
        //return $query->createCommand()->getRawSql();
        return $isAll ? $query->all() : $this->mongodbPage($query);
    }
}
